#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Drawing;
using System.IO;

namespace Grawlix.Services.Resources
{
	/// <summary>
	/// A typed resolver that handles images.
	/// </summary>
	public class ImageResolver :
		TypedResolverBase<WrappedImage>
	{
		/// <summary>
		/// Initializes a new instance of the 
		/// <see cref="ImageResolver"/> class.
		/// </summary>
		/// <param name="resolver">The resolver which accesses
		/// resources.</param>
		public ImageResolver(IUriResolver resolver)
			: base(resolver)
		{
		}

		/// <summary>
		/// Converts a resource object into the appropriate form.
		/// </summary>
		/// <param name="uri">The uri that identifies the typed
		/// resource.</param>
		/// <param name="resource">The untyped resource data.</param>
		/// <returns>The typed resource.</returns>
		/// <exception cref="IOException">A generic I/O error occurred.
		/// </exception>
		/// <exception cref="FileNotFoundException">The resource is
		/// the wrong type.
		/// </exception>
		/// <remarks>This is a blocking operation which should only be
		/// called asynchronously.</remarks>
		public override WrappedImage Convert(Uri uri,
		                                     object resource)
		{
			var imageResource = resource as Image;
			if (imageResource != null)
				return new WrappedImage(imageResource);
			return base.Convert(uri, resource);
		}

		/// <summary>
		/// Parses the specified resource stream into the appropriate
		/// type.
		/// </summary>
		/// <param name="resourceStream">The resource stream to parse.</param>
		/// <returns>The created object.</returns>
		protected override WrappedImage Parse(
			Stream resourceStream)
		{
			return new WrappedImage(Image.FromStream(resourceStream));
		}
	}
}